package server.commonData;

import server.ServerEngin;
import server.msgBean.LoginMessage.UserInfo;

/**
 * 玩家数据层
 * (机器人，玩家等 数据层)
 * @author King
 *
 */
public abstract class Player
{
	/**玩家类型  1用户  2机器人**/
	public static final int USER= 1 ,ROBOT = 2;
	
	/**用户id  或者机器人id**/
	protected long id;
	/**类型  用于区分机器人还是玩家**/
	protected int type;
	/** 金币 **/
	protected int money;
	/** 所在房间名称 **/
	private String roomName;
	/** 桌子编号 **/
	private long tableId;
	/** 头像 **/
	protected int face;
	/** 元宝 **/
	protected int yuanbao;
	/** vip等级 **/
	protected int vipLevel;
	/**vip结束时间**/
	protected long vipEndTime;
	/**名称**/
	protected String name;
	/**胜利次数**/
	protected int winNum;
	/**失败次数**/
	protected int failNum;
	/**已充值的元宝数**/
	protected int rechargedGold;
	/**等级**/
	protected int level=1;
	/**魅力**/
	protected int meili;
	
	public Player()
	{
		this.name = System.currentTimeMillis()+"";
	}
	
	/**
	 * 获取用户所在的房间
	 * 
	 * @return null时 用户还没有进入房间
	 */
	public Room getRoom()
	{
		return ServerEngin.getInstance().getCache().getRoomManager()
				.getRoom(this.roomName);
	}
	
	/**
	 * 获取所在的桌子
	 * @return
	 */
	public Table getTable()
	{
		Room room = getRoom();
		if(room==null)
			return null;
		return room.getTable(this.tableId);
	}

	public void setRoomName(String roomName)
	{
		this.roomName = roomName;
	}
	

	public long getId() {
		return id;
	}

	public void setTableId(long tableId) {
		this.tableId = tableId;
	}

	public long getTableId() {
		return tableId;
	}

	public int getMoney() {
		return money;
	}

	public int getFace() {
		return face;
	}

	public int getYuanbao() {
		return yuanbao;
	}

	public int getVipLevel() {
		return vipLevel;
	}

	public String getName() {
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getType()
	{
		return type;
	}

	public int getWinNum() {
		return winNum;
	}

	public int getFailNum() {
		return failNum;
	}

	public int getRechargedGold() {
		return rechargedGold;
	}

	public void setRechargedGold(int rechargedGold) {
		this.rechargedGold = rechargedGold;
	}

	
	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public long getVipEndTime() {
		return vipEndTime;
	}

	/**
	 * 增减减少vip剩余时间
	 * @param vipEndTime正数代表增加  负数代表减少 
	 */
	public void addDelVipEndTime(int vipEndTime)
	{
		this.vipEndTime += vipEndTime;
		if(this.vipEndTime<0)
			this.vipEndTime = 0;
	}
	
	/**
	 * 增加金币
	 * @param money
	 */
	public void addMoney(int money)
	{
		this.money +=money;
	}
	
	/**
	 * 减少金币
	 * @param money
	 * @return
	 */
	public int delMoney(int money)
	{
		if(this.money<money)
		{
			this.money = 0;
			return this.money;
		}
		this.money-=money;
		return money;
	}
	
	
	/**
	 * 增减减少元宝 
	 * 不刷新客户端
	 * @param yuanbao 正数代表增加  负数代表减少 
	 */
	public void addDelYuanbao(int yuanbao)
	{
		this.yuanbao+=yuanbao;
		if(this.yuanbao<0)
			this.yuanbao = 0;
	}
	
	public void addWinNum()
	{
		this.winNum++;
	}
	
	public void addFailNum()
	{
		this.failNum++;
	}
	
	/**显示桌上玩家信息**/
	public UserInfo.Builder showTablePlayerInfo()
	{
		UserInfo.Builder builder = UserInfo.newBuilder();
		builder.setVipLevel(this.getVipLevel());
		builder.setMoney(this.getMoney());
		builder.setName(this.getName());
		builder.setFaceIndex(this.getFace());
		builder.setUserId(this.getId());
		return builder;
	}
	
}
